﻿// Copyright 2006 - Ricardo Stuven (rstuven@gmail.com)
//
// This file is part of MsSqlSpatial.
// MsSqlSpatial is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// MsSqlSpatial is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with MsSqlSpatial; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System.Data.SqlTypes;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.IO;

namespace MsSqlSpatial
{
    public class ConvertGeometry
    {
        public static IGeometry ToGeometry(SqlBytes ewkb)
        {
            if (!ewkb.IsNull)
            {
                try
                {
                    var reader = new MsSqlSpatialReader();
                    return reader.Read(ewkb.Value);
                }
                catch
                {
                }
            }
            return null;
        }

        public static IGeometry ToGeometryFromWKB(byte[] wkb)
        {
            try
            {
                var reader = new WKBReader();
                IGeometry geometry = reader.Read(wkb);
                geometry.SRID = -1;
                return geometry;
            }
            catch
            {
            }
            return null;
        }

        internal static IGeometry ToGeometryFromWKB(SqlBytes wkb)
        {
            if (!wkb.IsNull)
            {
                try
                {
                    var reader = new WKBReader();
                    return reader.Read(wkb.Value);
                }
                catch
                {
                }
            }
            return null;
        }

        public static IGeometry ToGeometry(SqlChars wkt, SqlInt32 srid)
        {
            if (!wkt.IsNull)
            {
                IGeometry geometry = new WKTReader().Read(wkt.ToSqlString().Value);
                geometry.SRID = (srid.IsNull ? -1 : srid.Value);
                return geometry;
            }
            return null;
        }

        public static SqlBytes ToSqlBytes(IGeometry geometry)
        {
            if (geometry != null)
            {
                try
                {
                    return new SqlBytes(new MsSqlSpatialWriter().Write(geometry));
                }
                catch
                {
                }
            }
            return SqlBytes.Null;
        }

        public static SqlBytes ToSqlBytes(IGeometry geometry, SqlInt32 srid)
        {
            int sridValue;
            if (srid.IsNull)
                sridValue = -1;
            else
                sridValue = srid.Value;
            return ToSqlBytes(geometry, sridValue);
        }

        public static SqlBytes ToSqlBytes(IGeometry geometry, int srid)
        {
            if (geometry != null)
            {
                try
                {
                    geometry.SRID = srid;
                    return new SqlBytes(new MsSqlSpatialWriter().Write(geometry));
                }
                catch
                {
                }
            }
            return SqlBytes.Null;
        }
    }
}